package com.ott.tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ExecShell {
	public Boolean execShell(String path) {
		Runtime r = Runtime.getRuntime();
		try {
			
			r.exec("chmod -R 755 /opt/initver/*");
			Process p = r.exec("bash "+path);
	
	
			ProcessOutputThread p1 = new ProcessOutputThread(p.getInputStream());
			ProcessOutputThread p2 = new ProcessOutputThread(p.getErrorStream());
			p1.start();
			p2.start();
			try {
				if (p.waitFor() > -1) {
					System.out.println("完成sh文件执行！");
					return true;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return null;
	}

	
//	public static void execShell2(String path){
//		InputStreamReader stdISR = null;  
//        InputStreamReader errISR = null;  
//        Process process = null;  
//        String command = path;  
//        try {  
//            process = Runtime.getRuntime().exec(command);  
//            int exitValue = process.waitFor();  
//  
//            String line = null;  
//  
//            stdISR = new InputStreamReader(process.getInputStream());  
//            BufferedReader stdBR = new BufferedReader(stdISR);  
//            while ((line = stdBR.readLine()) != null) {  
//                System.out.println("STD line:" + line);  
//            }  
//  
//            errISR = new InputStreamReader(process.getErrorStream());  
//            BufferedReader errBR = new BufferedReader(errISR);  
//            while ((line = errBR.readLine()) != null) {  
//                System.out.println("ERR line:" + line);  
//            }  
//        } catch (IOException | InterruptedException e) {  
//            e.printStackTrace();  
//        } finally {  
//            try {  
//                if (stdISR != null) {  
//                    stdISR.close();  
//                }  
//                if (errISR != null) {  
//                    errISR.close();  
//                }  
//                if (process != null) {  
//                    process.destroy();  
//                }  
//            } catch (IOException e) {  
//                System.out.println("正式执行命令：" + command + "有IO异常");  
//            }  
//        }  
//	}
	
	
	class  ProcessOutputThread extends Thread {
		private InputStream is;


		public ProcessOutputThread(InputStream is) throws IOException {
			if (null == is) {
				throw new IOException("the provided InputStream is null");
			}
			this.is = is;
		}


		@Override
		public void run() {
			InputStreamReader ir = null;
			BufferedReader br = null;
			try {
				ir = new InputStreamReader(this.is);
				br = new BufferedReader(ir);
				String output = null;
				while (null != (output = br.readLine())) {
					System.out.println(output);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (null != br) {
						br.close();
					}
					if (null != ir) {
						ir.close();
					}
					if (null != this.is) {
						this.is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
